import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import CubicSpline

# 生成不规则的温度数据
np.random.seed(42)  # 固定随机种子
time_points = np.sort(np.random.choice(np.linspace(0, 24, 50), 12, replace=False))  # 随机12个时间点，范围0到24小时
temperature_points = np.sin(time_points * np.pi / 12) * 10 + np.random.normal(0, 1, len(time_points))  # 温度数据模拟

# 使用样条插值进行数据补全
cs = CubicSpline(time_points, temperature_points)  # 创建样条插值对象

# 生成插值点
time_fine = np.linspace(0, 24, 200)
temperature_fine = cs(time_fine)
temperature_derivative2 = cs(time_fine, 2)  # 计算二阶导数

# 设置画布和子图布局
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10), sharex=True)

# 图形1：原始数据点与样条插值曲线
ax1.plot(time_fine, temperature_fine, label="Cubic Spline Interpolation", color="blue", linewidth=2)
ax1.scatter(time_points, temperature_points, color="red", label="Original Data Points", s=50)
ax1.set_title("Temperature over Time with Cubic Spline Interpolation")
ax1.set_ylabel("Temperature (°C)")
ax1.legend()
ax1.grid(True)

# 图形2：样条插值后温度的二阶导数（加速度）
ax2.plot(time_fine, temperature_derivative2, label="Second Derivative (Acceleration)", color="green", linewidth=2)
ax2.set_title("Second Derivative of Temperature (Acceleration)")
ax2.set_xlabel("Time (hours)")
ax2.set_ylabel("Acceleration of Temperature Change")
ax2.legend()
ax2.grid(True)

plt.tight_layout()
plt.show()
